package org.flashbrighton.as3bots.effects {
	/**
	 * ...
	 * @author Jaime Domínguez
	 */
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class LineParticle extends Sprite {
		
		private var speed:Number;
		private var lineAlpha:Number;
		private var vectX:Number;
		private var vectY:Number;
		private var lastX:Number=0;
		private var lastY:Number=0;
		private var lineColour:Number = 0;
		
		public function LineParticle(rotation:int,colour:Number,_speed:Number) {
			init(rotation,colour,_speed);
			lineAlpha = Math.random();
		}
		
		
		private function init(rotation:int,colour:Number,_speed:Number):void {
			lineColour = colour;
			speed = Math.random() * _speed;
			//make this the radious of the bot.
			lastY = lastX = 15;
			//getSpeed();
			getVector(rotation);
			updater();
			//initialExplosion();
		} 
		
		private function initialExplosion():void{
			this.graphics.lineStyle(1,lineColour,0.5);
			this.graphics.moveTo(0, 0);
			this.graphics.drawCircle(0, 0, lastX);
		}
		
		private function updater():void{
			this.addEventListener(Event.ENTER_FRAME, draw, false, 0, true);
		}
		
		
		private function draw(e:Event):void {
			this.graphics.clear();
			//the other way of drawing this effect is drawing and animating with OEF, all in horizontal. and then rotating the sprite holder. from the beginning.
			this.graphics.lineStyle(Math.random()*3*speed/100,lineColour,lineAlpha);
			//this.graphics.lineStyle(5,0xffffff);
			this.graphics.moveTo(lastX, lastY);
			lastX += vectX * speed;
			lastY += vectY * speed;
			speed*=0.90;
			lineAlpha-=0.01;
			this.graphics.lineTo(lastX,lastY);
			
			if (speed <= 1) {
				removeEventListener(Event.ENTER_FRAME, draw);
				this.graphics.clear();
				e.currentTarget.parent.removeChild(e.currentTarget);
				dispatchEvent(new Event(Event.REMOVED));
			}
			
		}
		
		
		private function getSpeed():void{
			speed = Math.random() * 40;
			//speed = 15;
		}
		
		
		private function getVector(rotation:int):void{
			vectX = Math.cos(rotation) - Math.sin(rotation);
			vectY = Math.sin(rotation) + Math.cos(rotation);
		}
		
	}

}